<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Map;

use ArrayIterator;
use InvalidArgumentException;
use Skyline\Comparator\ComparatorInterface;
use Skyline\Predicate\PredicateInterface;
use Skyline\Structure\ContainerInterface;

/**
 * A map that is backed by a plain old associative array
 * 
 * Note that non-scalar keys are not supported
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ArrayMap extends AbstractMap
{
	/**
	 * @var type 
	 */
	protected $changed = false;
	
	/**
	 * @var scalar[mixed]
	 */
	protected $entries = array();
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable 
	 * @return void
	 */
	public function __construct($entries = null)
	{
		if(!is_null($entries)) {
			$this->setAll($entries);
		}
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->entries = array();
	}
	
	/**
	 * Returns true if a given key exists in this map
	 * 
	 * @param mixed $key
	 * @return bool
	 */
	public function containsKey($key)
	{
		return array_key_exists($key, $this->entries);
	}
	
	/**
	 * Returns true if a given value exists in this map
	 * 
	 * @param mixed $value
	 * @return bool
	 */
	public function containsValue($value)
	{
		foreach($this->entries as $v) {
			if($v === $value) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns the value associated with a given key (if any)
	 * 
	 * @param mixed $key
	 * @return mixed|null Returns null if key not in map
	 */
	public function get($key)
	{
		if($this->containsKey($key)) {
			return $this->entries[$key];
		}
	}
	
	/**
	 * Returns a set of the keys in this map
	 * @return SetInterface<mixed>
	 */
	public function getKeys()
	{
		return $this->createSet(array_keys($this->entries));
	}
	
	/**
	 * Returns a set of the values in this map
	 * @return SetInterface<mixed>
	 */
	public function getValues()
	{
		return $this->createSet(array_values($this->entries));
	}
	
	/**
	 * Returns the first key associated with a given value (if any)
	 * 
	 * @param mixed $value
	 * @return mixed|null Returns null if no key is associated with the value
	 */
	public function keyOf($value)
	{
		foreach($this->entries as $key => $v) {
			if($v === $value) {
				return $key;
			}
		}
	}
	
	/**
	 * Returns all keys associated with a given value
	 * 
	 * @param mixed $value
	 * @return mixed[]
	 */
	public function keysOf($value)
	{
		$keys = array();
		foreach($this->entries as $key => $v) {
			if($v === $value) {
				$keys[] = $key;
			}
		}
		return $keys;
	}
	
	/**
	 * Removes a key from the map
	 * 
	 * @param mixed $key
	 * @return void
	 */
	public function remove($key)
	{
		unset($this->entries[$key]);
		$this->changed = true;
	}
	
	/**
	 * Associates a given key with a given value in the map
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return void
	 */
	public function set($key, $value)
	{
		if(!is_scalar($key)) {
			throw new InvalidArgumentException('Only scalar keys are supported by ArrayMap');
		}
		$this->assertNotFull();
		$this->assertEntryIsValid($key, $value);
		$this->entries[$key] = $value;
		$this->changed = true;
	}
	
	/**
	 * Returns this map in the form of an associative array (if possible)
	 * 
	 * @throws LogicException If operation is unsupported
	 * @return mixed[mixed]
	 */
	public function toArray()
	{
		$this->sortEntries();
		return $this->entries;
	}
	
	/**
	 * Returns the number of entries in the map
	 * @return int
	 */
	public function count()
	{
		return count($this->entries);
	}
	
	/**
	 * Returns an iterator for the entries in the map
	 * @return Iterator
	 */
	public function getIterator()
	{
		return new ArrayIterator($this->toArray());
	}
	
	/**
	 * Sorts the entries by their keys
	 * @return void
	 */
	protected function sortEntries()
	{
		if(!is_null($this->comparator) && $this->changed) {
			uksort($this->entries, array($this->comparator, 'compare'));
			$this->changed = false;
		}
	}
}